package net.crazyblock.mobilesafe.engin;

import java.util.ArrayList;
import java.util.List;

import net.crazyblock.mobilesafe.domain.AppInfo;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;


public class AppInfoProvider {

	public static List<AppInfo> getAppinfos(Context context) {
		PackageManager pm = context.getPackageManager();
		List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
		List<AppInfo> appInfos = new ArrayList<AppInfo>();

		for (PackageInfo packageInfo : packageInfos) {
			String packName = packageInfo.packageName;
			String name = packageInfo.applicationInfo.loadLabel(pm).toString();
			Drawable icon = packageInfo.applicationInfo.loadIcon(pm);

			AppInfo appInfo = new AppInfo();
			appInfo.setPackName(packName);
			appInfo.setName(name);
			appInfo.setIcon(icon);
			
			int flags = packageInfo.applicationInfo.flags;
			if ((flags&ApplicationInfo.FLAG_SYSTEM)==0) {
				//user app
				appInfo.setUserApp(true);
			} else {
				//system app
				appInfo.setUserApp(false);
			}
			if ((flags&ApplicationInfo.FLAG_EXTERNAL_STORAGE)==0) {
				//in rom
				appInfo.setInRom(true);
			} else {
				//extr storage
				appInfo.setInRom(false);
			}

			appInfos.add(appInfo);
		}

		return appInfos;
	}
}
